#include "imitateplay.h"

ImitatePlay::ImitatePlay(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口
    //固定尺寸
    this->setFixedSize(mainSceneWidth,mainSceneHeight);
    //标题
    this->setWindowTitle(mainSceneName);
    //图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    PushButton_01 * back =new PushButton_01(":/res/BackButton.png",":/res/BackButtonSelected.png");
    back->setParent(this);
    back->move(this->width() - back->width(),this->height() - back->height());
    connect(back,&PushButton_01::clicked,[=](){
        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->ImitateSceneBack();
        });
    });

    //作弊按钮1
    PushButton_01 * chick1 =new PushButton_01(":/res/wyzg.png",":/res/wyzg.png");
    chick1->setParent(this);
    chick1->move(this->width() - chick1->width()-100,this->height() - chick1->height());
    connect(chick1,&PushButton_01::clicked,[=](){
        current = 1;
    });

    //作弊按钮2
    PushButton_01 * chick2 =new PushButton_01(":/res/syfg.png",":/res/syfg.png");
    chick2->setParent(this);
    chick2->move(this->width() - chick2->width()-200,this->height() - chick2->height());
    connect(chick2,&PushButton_01::clicked,[=](){
        current = 2;
    });

    //作弊按钮3
    PushButton_01 * chick3 =new PushButton_01(":/res/lyqg.png",":/res/lyqg.png");
    chick3->setParent(this);
    chick3->move(this->width() - chick3->width()-300,this->height() - chick3->height());
    connect(chick3,&PushButton_01::clicked,[=](){
        current = 3;
    });

    //当前关卡标题
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level: Imitate");
    label->setText(str);
    label->setGeometry(QRect(30,this->height() - 50, this->width(),50 ));
    label->setAttribute(Qt::WA_TransparentForMouseEvents);

    //当前步数
    QLabel * plabel = new QLabel;
    plabel->setParent(this);
    QFont pfont;
    pfont.setFamily("华文新魏");
    pfont.setPointSize(20);
    plabel->setFont(pfont);
    QString pstr = QString("pace: %1").arg(this->pace);
    plabel->setText(pstr);
    plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
    plabel->setAttribute(Qt::WA_TransparentForMouseEvents);

    //胜利图片
    QLabel * winLabel = new QLabel;
    winLabel->setParent(this);
    QPixmap pix;
    pix.load(":/res/xbdnhny.png");
    winLabel->setPixmap(pix);
    winLabel->setGeometry(0,0,pix.width(),pix.height());
    winLabel->move(this->width()*0.5-pix.width()*0.5,-pix.height());



    //初始化二维数组
    int arrayi1[4][4];
    int arrayi2[4][4];
    srand((unsigned)time(NULL));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            arrayi1[i][j] = (rand() % 2);
            this->gameArray[i][j] = arrayi1[i][j];
            arrayi2[i][j] = (rand() % 2);
        }
    }

    for(int i = 0 ; i < 4;i++)
    {
       for(int j = 0 ; j < 4; j++)
       {

           //绘制背景图片
            QLabel* label = new QLabel;
            QPixmap pix;
            pix.load(":/res/BoardNode(1).png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(100 + i*75,400+j*75);

            //创建金币
            QString str;
            if(this->gameArray[i][j] == 1){
                str = ":/res/Coin0001.png";
            }
            else
            {
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(100 + i*75,400+j*75);
            coin->posX=i;
            coin->posY=j;
            coin->flag = gameArray[i][j];

            this->coinBtn[i][j]=coin;

            //绘制背景图片
             QLabel* label1 = new QLabel;
             QPixmap pix1;
             pix1.load(":/res/BoardNode(1).png");
             label1->setGeometry(0,0,pix1.width(),pix1.height());
             label1->setPixmap(pix1);
             label1->setParent(this);
             label1->move(535 + i*75,400+j*75);

             //创建金币
             QString str1;
             if(arrayi2[i][j] == 1){
                 str1 = ":/res/Coin0001.png";
             }
             else
             {
                 str1 = ":/res/Coin0008.png";
             }
             MyCoin * coin1 = new MyCoin(str1);
             coin1->setParent(this);
             coin1->move(535 + i*75,400+j*75);
             coin1->posX=i;
             coin1->posY=j;
             coin1->flag = gameArray[i][j];

             //点击硬币调用
             connect(coin,&MyCoin::clicked,[=](){

                coin->changeFlag();

                //同步二维数组，方便后期扩展
                gameArray[i][j]=gameArray[i][j]==0?1:0;

                if(current==0)
                {

                    pace++;
                    QString pstr = QString("pace: %1").arg(this->pace);
                    plabel->setText(pstr);
                    plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
                    plabel->setAttribute(Qt::WA_TransparentForMouseEvents);

                    //翻转周围硬币
                    QTimer::singleShot(300,this,[=](){
                        //右侧
                        if(coin->posX+1<=3)
                        {
                            this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                            gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]==0?1:0;
                        }
                        //左侧
                        if(coin->posX-1>=0)
                        {
                            this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                            gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]==0?1:0;
                        }
                        //下侧
                        if(coin->posY+1<=3)
                        {
                            this->coinBtn[coin->posX][coin->posY+1]->changeFlag();
                            gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]==0?1:0;
                        }
                        //上侧
                        if(coin->posY-1>=0)
                        {
                            this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                            gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==0?1:0;
                        }

                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                if(gameArray[i][j]==arrayi2[i][j])
                                {
                                    this->coinBtn[i][j]->isWin = true;
                                }
                                else if(gameArray[i][j]!=arrayi2[i][j])
                                {
                                    this->coinBtn[i][j]->isWin = false;
                                }
                            }
                        }

                        //检测游戏是否胜利
                        this->isWin = true;
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                if(this->coinBtn[i][j]->isWin==false)
                                {
                                    this->isWin=false;
                                    break;
                                }
                            }
                        }

                        if(this->isWin)
                        {
                            QPropertyAnimation *animation = new QPropertyAnimation(winLabel,"geometry");
                            animation->setDuration(1000);
                            animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                            animation->setEndValue(QRect(winLabel->x(),winLabel->y()+814,winLabel->width(),winLabel->height()));
                            animation->setEasingCurve(QEasingCurve::OutBounce);
                            animation->start(QAbstractAnimation::DeleteWhenStopped);

                            if(pace<=10)
                            {
                                QMessageBox msgbox;
                                msgbox.setText("您被封为：咸鱼（翻深）之王");
                                msgbox.exec();
                            }
                            else if(pace>10&&pace<=30)
                            {
                                QMessageBox msgbox;
                                msgbox.setText("您被封为：咸鱼（翻深）宝宝");
                                msgbox.exec();
                            }
                            else
                            {
                                QMessageBox msgbox;
                                msgbox.setText("咸鱼，你的功力还不足噢！");
                                msgbox.exec();
                            }

                            for(int i=0;i<4;i++)
                            {
                                for(int j=0;j<4;j++)
                                {
                                    this->coinBtn[i][j]->isWin=true;
                                }
                            }
                        }
                        else
                        {
                            for(int i=0;i<4;i++)
                            {
                                for(int j=0;j<4;j++)
                                {
                                    this->coinBtn[i][j]->isWin = false;
                                }
                            }
                        }
                    });
                }
             });
       }
    }

}
//绘图事件
void ImitatePlay::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bfda0be809541c57e1407952d1e6042.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}
